home *** CD-ROM | disk | FTP | other *** search
/ PC Graphics Unleashed / PC Graphics Unleashed.iso / ch09 / drawgrid.c < prev    next >
C/C++ Source or Header  |  1994-09-15  |  6KB  |  294 lines

  1. // DRAWGRID.C
  2. #include <conio.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <graphics.h>
  6. #include <math.h>
  7.  
  8. #define TOL 0.0005
  9. #define PI  3.14159265358979323846
  10.  
  11. typedef struct {
  12.  float x,y,z;
  13. } POINT3D;
  14.  
  15. typedef struct {
  16.  int x,y;
  17. } POINTint;
  18.  
  19.  
  20. /* World Limits in 2D */
  21. float WXleft;
  22. float WXright;
  23. float WYtop;
  24. float WYbottom;
  25.  
  26. /* Device Limits */
  27. int DXmin;
  28. int DYmin;
  29. int DYmax;
  30. int DXmax;
  31.  
  32. /* Rotations for 3D View in Radians*/
  33. float RX;
  34. float RY;
  35. float RZ;
  36. float COSRX;
  37. float SINRX;
  38. float COSRY;
  39. float SINRY;
  40. float COSRZ;
  41. float SINRZ;
  42.  
  43.  
  44.  
  45. void InitGraphics(void);
  46. float DegToRad(float deg);
  47. float RadToDeg(float rad);
  48. POINT3D World3DToWorld2D(POINT3D p);
  49. POINTint World2DToDevice(POINT3D p);
  50. void drawpoint(POINT3D p1);
  51. void drawline(POINT3D p1,POINT3D p2);
  52. void drawaxis(void);
  53. void SetAxesAngles(float rx, float ry, float rz);
  54. void drawgrid (void);
  55. void UpdateGridStatus(void);
  56.  
  57. typedef struct {
  58.    float rmin, cmin,
  59.      rowstep,colstep;
  60.    int rows, cols;
  61. } GRID;
  62.  
  63. GRID g;
  64. int GridRowCol;
  65.  
  66. void main()
  67. {
  68.   POINT3D p;
  69.   InitGraphics();
  70.  
  71.   /* World Limits */
  72.   WXleft   = -1.5;
  73.   WXright  =  1.5;
  74.   WYtop    =  1.5;
  75.   WYbottom = -1.5;
  76.  
  77.   /* Device Limits On A Viewport */
  78.   DXmin = 0.0;
  79.   DYmin = 0.0;
  80.   DYmax = getmaxy()/2;
  81.   DXmax = getmaxy()/2;
  82.  
  83.   /* Draw A 2X2 Grid */
  84.   setcolor(WHITE);
  85.   rectangle(0,0,getmaxy(),getmaxy());
  86.   line(getmaxy()/2,0,getmaxy()/2,getmaxy());
  87.   line(0,getmaxy()/2,getmaxy(),getmaxy()/2);
  88.  
  89.   /* Initialize a 3D point */
  90.   p.x = 0.5;
  91.   p.y = 0.2;
  92.   p.z = 0.5;
  93.  
  94.   GridRowCol = 6;
  95.  
  96.   /* TOP VIEW */
  97.   setviewport(0,0,getmaxy()/2,getmaxy()/2,1);
  98.   SetAxesAngles(90, 0, 0);
  99.   UpdateGridStatus();
  100.   drawgrid();
  101.   drawaxis();
  102.   drawpoint(p);
  103.  
  104.   /* ISO VIEW */
  105.   setviewport(getmaxy()/2,0,getmaxy(),getmaxy()/2,1);
  106.   SetAxesAngles(20, -15, -5);
  107.   UpdateGridStatus();
  108.   drawgrid();
  109.   drawaxis();
  110.   drawpoint(p);
  111.  
  112.   /* FRONT VIEW */
  113.   setviewport(0,getmaxy()/2,getmaxy()/2,getmaxy(),1);
  114.   SetAxesAngles(0, 0, 0);
  115.   UpdateGridStatus();
  116.   drawgrid();
  117.   drawaxis();
  118.   drawpoint(p);
  119.  
  120.   /* SIDE VIEW */
  121.   setviewport(getmaxy()/2,getmaxy()/2,getmaxy(),getmaxy(),1);
  122.   SetAxesAngles(0, -90, 0);
  123.   UpdateGridStatus();
  124.   drawgrid();
  125.   drawaxis();
  126.   drawpoint(p);
  127.  
  128.   getch();
  129.   closegraph();
  130. }
  131.  
  132. /* /////////////////////////////////////////////////////// */
  133. void drawgrid (void)
  134. {
  135.    int j;
  136.    float rmax,cmax;
  137.    POINT3D p1,p2;
  138.    rmax = g.rmin+g.rowstep*(g.rows);
  139.    cmax = g.cmin+g.colstep*(g.cols);
  140.    setcolor(LIGHTGRAY);
  141.    for (j=0;j<g.cols+1;j++) {
  142.      p1.x = g.cmin+j*g.colstep;
  143.      p1.y = rmax;
  144.      p1.z = 0.0;
  145.      p2.x = g.cmin+j*g.colstep;
  146.      p2.y = g.rmin,
  147.      p2.z = 0.0;
  148.      drawline(p1,p2);
  149.    }
  150.    for (j=0;j<g.rows+1;j++) {
  151.      p1.x = g.cmin;
  152.      p1.y = g.rmin+j*g.rowstep;
  153.      p1.z = 0.0;
  154.      p2.x = cmax;
  155.      p2.y = g.rmin+j*g.rowstep;
  156.      p2.z = 0.0;
  157.      drawline(p1,p2);
  158.    }
  159.    setcolor(WHITE);
  160. }
  161.  
  162. void UpdateGridStatus(void)
  163. {
  164.   g.rmin = WXleft;
  165.   g.cmin = WYbottom;
  166.   g.rowstep = (fabs(WYbottom)+ fabs(WYtop)) / (float)GridRowCol;
  167.   g.colstep = (fabs(WXleft)+ fabs(WXright)) / (float)GridRowCol;
  168.   g.rows = GridRowCol;
  169.   g.cols = GridRowCol;
  170. }
  171.  
  172.  
  173. void InitGraphics(void)
  174. {
  175.  int gdriver = DETECT, gmode, errorcode;
  176.  initgraph(&gdriver, &gmode, "");
  177.  if (gdriver != VGA) {
  178.   printf("VGA graphics card required.\n");
  179.   exit(1);
  180.  }
  181.  errorcode = graphresult();
  182.  if (errorcode != grOk)  /* an error occurred */
  183.  {
  184.    printf("Graphics error: %s\n", grapherrormsg(errorcode));
  185.    printf("Press any key to halt:");
  186.    getch();
  187.    exit(1); /* terminate with an error code */
  188.  }
  189.  setviewport(0,0,getmaxx(),getmaxy(),1);
  190. }
  191.  
  192. float DegToRad(float deg)
  193. {
  194.  return(deg*PI/180.0);
  195. }
  196.  
  197. float RadToDeg(float rad)
  198. {
  199.  return(rad*180.0/PI);
  200. }
  201.  
  202. void SetAxesAngles(float rx, float ry, float rz) {
  203.     RX = DegToRad(rx);
  204.     RY = DegToRad(ry);
  205.     RZ = DegToRad(rz);
  206.     COSRX = cos(RX);
  207.     SINRX = sin(RX);
  208.     COSRY = cos(RY);
  209.     SINRY = sin(RY);
  210.     COSRZ = cos(RZ);
  211.     SINRZ = sin(RZ);
  212. }
  213.  
  214.  
  215.  
  216. POINT3D World3DToWorld2D(POINT3D p)
  217. {
  218.   POINT3D ptemp;
  219.   ptemp = p;
  220.   if (RX) {
  221.     ptemp.x  = p.x;
  222.     ptemp.y  = COSRX*p.y - SINRX*p.z;
  223.     ptemp.z  = SINRX*p.y + COSRX*p.z;
  224.     p = ptemp;
  225.   }
  226.   if (RY) {
  227.     ptemp.x  = COSRY*p.x + SINRY*p.z;
  228.     ptemp.y  = p.y;
  229.     ptemp.z  = -SINRY*p.x + COSRY*p.z;
  230.     p = ptemp;
  231.   }
  232.   if (RZ) {
  233.     ptemp.x  = COSRZ*p.x - SINRZ*p.y;
  234.     ptemp.y  = SINRZ*p.x + COSRZ*p.y;
  235.     ptemp.z  = p.z;
  236.   }
  237.   if (fabs(ptemp.x) < TOL)  ptemp.x = 0.0;
  238.   if (fabs(ptemp.y) < TOL)  ptemp.y = 0.0;
  239.   if (fabs(ptemp.z) < TOL)  ptemp.z = 0.0;
  240.   return(ptemp);
  241. }
  242.  
  243. POINTint World2DToDevice(POINT3D p)
  244. {
  245.  
  246.  POINTint ptemp;
  247.  ptemp.x = (WXleft-p.x)*(DXmax-DXmin)/(WXleft-WXright) + DXmin + 0.5;
  248.  ptemp.y = (WYtop-p.y)*(DYmax-DYmin)/(WYtop-WYbottom) + DYmin + 0.5;
  249.  return(ptemp);
  250.  
  251. }
  252.  
  253. void drawpoint(POINT3D p1)
  254. {
  255.  /* draws a 3D point   */
  256.  POINTint p2;
  257.  p1.z = -p1.z;
  258.  p2 = World2DToDevice(World3DToWorld2D(p1));
  259.  //circle(p2.x,p2.y,2);
  260.  rectangle(p2.x - 2, p2.y - 2, p2.x + 2, p2.y + 2);
  261. }
  262.  
  263. void drawline(POINT3D p1,POINT3D p2)
  264. {
  265.  /* draws a 3D line  */
  266.   POINTint p11,p22;
  267.   p1.z = -p1.z;
  268.   p2.z = -p2.z;
  269.   p11  = World2DToDevice(World3DToWorld2D(p1));
  270.   p22  = World2DToDevice(World3DToWorld2D(p2));
  271.   line(p11.x,p11.y,p22.x,p22.y);
  272. }
  273.  
  274. void drawaxis(void) {
  275.   POINT3D p1,p2;
  276.  
  277.   p1.x = p1.y = p1.z = 0;
  278.  
  279.   p2.x = 1; p2.y = 0; p2.z=0;
  280.   setcolor(RED);
  281.   drawline(p1,p2);
  282.  
  283.   p2.x = 0; p2.y = 1; p2.z=0;
  284.   setcolor(GREEN);
  285.   drawline(p1,p2);
  286.  
  287.   p2.x = 0; p2.y = 0; p2.z=1;
  288.   setcolor(BLUE);
  289.   drawline(p1,p2);
  290.  
  291.   setcolor(WHITE);
  292. }
  293.  
  294.